package com.example.base.tools.config;

import android.content.Context;
import android.os.Bundle;
import android.util.Log;

import com.example.base.tools.tools.PackageUtils;

public class CorePreferences {


    private static CorePreferences corePreferences = null;

    private static CoreConfig coreConfig;

    public static final String CACHEPATH = "cache";

    public static final String IMAGEPATH = "image";

    public static final String IMAGEPATH1 = "ImgCach";

    public static final String TEMPPATH = "temp";

    public static final String DOWNLOADPATH = "download";

    public static int avgpage = 20;

    public static Context ctx;

    public CorePreferences(Context context)
    {
        coreConfig = initConfig(context);
    }

    public static CorePreferences getInstance(Context context)
    {
        if (corePreferences == null)
        {
            return new CorePreferences(context);
        }
        return corePreferences;
    }

    public CoreConfig getCoreConfig()
    {
        return coreConfig;
    }

    /***
     * 获取Manifest.xml中配置的meta
     *
     * @param context
     * @return
     */
    public CoreConfig initConfig(Context context)
    {
        ctx = context;
        if (coreConfig == null)
        {
            Bundle metaBundle = PackageUtils.getAppMetaData(context);
            if (metaBundle != null)
            {
                coreConfig = new CoreConfig();
                coreConfig.setAppName(metaBundle.getString("app_name"));
                coreConfig.setAppTag(metaBundle.getString("app_tag"));
            }
        }
        return coreConfig;
    }


    /***
     * @param s
     */
    public static final void DEBUG(String s)
    {
        if (coreConfig.isDebug())
        {
            Log.i(coreConfig.getAppTag(), s);
        }
    }

    public static final void ERROR(Throwable e)
    {
        if (coreConfig.isDebug())
        {
            Log.e(coreConfig.getAppTag(), e.getMessage(), e);
        }
    }

    public static final void ERROR(String s)
    {
        if (coreConfig.isDebug())
        {
            Log.e(coreConfig.getAppTag(), s);
        }
    }

    public static final void ERROR(String s, Throwable e)
    {
        if (coreConfig.isDebug())
        {
            Log.e(coreConfig.getAppTag(), s, e);
        }
    }

    public static int getAvgpage()
    {
        return avgpage;
    }

    public static void setAvgpage(int avgpage)
    {
        CorePreferences.avgpage = avgpage;
    }

    public static boolean isDebug()
    {
        return coreConfig.isDebug();
    }

    public static String getAnalyseChannel()
    {
        return coreConfig.getAnalyseChannel();
    }
}


